﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcProject.Models;
using MvcProject.Utils;
using Webdiyer.WebControls.Mvc;
using System.Transactions;

namespace MvcProject.Controllers
{
    [Authorize]
    public class ProjectInfoController : Controller
    {
        private ProjectInfoModel db = new ProjectInfoModel();
        //
        // GET: /ProjectInfo/

        //public ActionResult Index()
        //{
        //    var dinners = db.FindAllProjectInfos().ToList();
        //    return View(dinners);
        //}
        public ActionResult Index(int? page)
        {
            PagedList<ProjectInfo> orders = db.FindAllProjectInfos().ToPagedList(page ?? 1, 10);
            return View(orders);
        }
        //
        // GET: /ProjectInfo/Details/5

        public ActionResult Details(int id)
        {
            ProjectInfo dinner = db.GetProjectInfo(id);
            ContractInfoModel db1 = new ContractInfoModel();
            IEnumerable<MvcProject.Models.ContractInfo> ContractInfos = db1.FindAllContractInfosByForeignerId(id);
            ViewData["ContractInfos"] = ContractInfos;

            BaseModels bm = new BaseModels();
            IEnumerable<MvcProject.Models.Team> Teams = bm.FindAllTeamsByProjectId(id);
            ViewData["Teams"] = Teams;
            return View(dinner);
        }

        //
        // GET: /ProjectInfo/Create
        public ActionResult Create()
        {
            return View("Create");
        }
        //
        // POST: /ProjectInfo/Create

        [HttpPost]
        public ActionResult Create(ProjectInfo dinner)
        {
            if (ModelState.IsValid)
                try
                {
                    UpdateModel(dinner);
                    dinner.CreateDate = System.DateTime.Now;
                    dinner.CreateUserName = HttpContext.User.Identity.Name;
                    db.Add(dinner);
                    db.Save();
                    return RedirectToAction("Details", new { id = dinner.ProjectId });
                }
                catch (Exception e)
                {
                    return View(dinner);
                }
            return View(dinner);
        }

        //
        // GET: /ProjectInfo/Edit/5

        public ActionResult Edit(int id)
        {
            ProjectInfo dinner = db.GetProjectInfo(id);
            return View(dinner);
        }

        //
        // POST: /ProjectInfo/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            ProjectInfo dinner = db.GetProjectInfo(id);
            UpdateModel(dinner);
            dinner.CreateDate = System.DateTime.Now;
            db.Save();
            return RedirectToAction("Details", new { id = dinner.ProjectId });
        }

        //
        // GET: /ProjectInfo/Delete/5

        public ActionResult Delete(int id)
        {
            ProjectInfo dinner = db.GetProjectInfo(id);
            if (dinner == null)
                return View("NotFound");
            else
                return View(dinner);
        }

        //
        // POST: /ProjectInfo/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            ProjectInfo dinner = db.GetProjectInfo(id);
            if (dinner == null)
                return View("NotFound");
            using (TransactionScope ts = new TransactionScope())
            {
                try
                {
                    //删除合同
                    ContractInfoModel cim = new ContractInfoModel();
                    cim.FindAllContractInfosByForeignerId(id);
                    //删除班组           
                    BaseModels bm = new BaseModels();
                    bm.DeleteAllTeamByProjectId(id);
                    //删除项目
                    db.Delete(dinner);
                    db.Save();
                    ts.Complete();
                }
                catch(Exception ex)
                {
                    throw ex;
                }
            }

            return View("Index");
        }
    }
}
